﻿using System;
using System.Data;
using System.Globalization;
using System.Linq;
using Microsoft.SharePoint.WebControls;

namespace ZhukBlogSharePointProject.CustomEntityPicker
{
    public class CustomSimpleQueryControl : SimpleQueryControl
    {
        public TableResultControl ResultControl
        {
            get { return (TableResultControl)PickerDialog.ResultControl; }
        }

        public CustomEntityEditorWithPicker EditorControl
        {
            get { return (CustomEntityEditorWithPicker)PickerDialog.EditorControl; }
        }

        public string RequestedState
        {
            get
            {
                return string.IsNullOrEmpty(Page.Request.QueryString["State"])
                    ? "TN"
                    : Page.Request.QueryString["State"];
            }
        }

        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
            ColumnList.Visible = false;
        }

        private static DataTable CreateDataTable()
        {
            var table = new DataTable { Locale = CultureInfo.InvariantCulture };
            table.Columns.Add(new DataColumn { DataType = typeof(string), ColumnName = "Id" });
            table.Columns.Add(new DataColumn { DataType = typeof(string), ColumnName = "Title" });
            table.Columns.Add(new DataColumn { DataType = typeof(string), ColumnName = "Description" });
            table.Columns.Add(new DataColumn { DataType = typeof(string), ColumnName = "Date" });
            return table;
        }

        public override PickerEntity GetEntity(DataRow dataRow)
        {
            if (dataRow == null)
                return null;
            var entity = new PickerEntity
            {
                Key = Convert.ToString(dataRow["Id"]),
                DisplayText = Convert.ToString(dataRow["Title"]),
                Description = Convert.ToString(dataRow["Description"]),
                IsResolved = true
            };
            entity.EntityData.Clear();
            entity.EntityData.Add("Date", dataRow["Date"]);
            entity.EntityData.Add("EntityType", "CustomObject");
            return entity;
        }

        protected override int IssueQuery(string search, string group, int pageIndex, int pageSize)
        {
            search = (search != null) ? search.Trim() : null;

            if (string.IsNullOrEmpty(search))
            {
                PickerDialog.ErrorMessage = "Негде искать";
                return 0;
            }
            var objData = new FakeDataRepository();
            var objs = objData.GetObjects(search);
            if (objs == null || objs.Count() == 0)
            {
                PickerDialog.ErrorMessage = "Ничего не найдено";
                return 0;
            }
            var table = CreateDataTable();
            foreach (var obj in objs)
            {
                var row = table.NewRow();
                row["Id"] = obj.Id;
                row["Title"] = obj.Title;
                row["Description"] = obj.Description;
                row["Date"] = obj.Date;
                table.Rows.Add(row);
            }
            PickerDialog.Results = table;
            PickerDialog.ResultControl.PageSize = table.Rows.Count;
            var rc = PickerDialog.ResultControl as TableResultControl;
            if (rc != null)
            {
                rc.ColumnNames.AddRange(new[] { "Id", "Title", "Description", "Date" });
                rc.ColumnDisplayNames.AddRange(new[] { "ИД", "Название", "Описание", "Дата" });
                rc.ColumnWidths.AddRange(new[] { "10%", "30%", "40%", "20%" });
            }
            return table.Rows.Count;
        }
    }
}
